package com.oazon.moco.lan;

import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.oazon.common.Settings;
import com.oazon.common.util.ServiceUtil;
import com.oazon.common.util.Utils;
import com.oazon.moco.lan.core.controldriver.cache.ControlDriverCache;
import com.oazon.moco.lan.core.http.HttpJsonServerService;
import com.oazon.moco.lan.core.nsd.NsdService;
import com.oazon.moco.lan.core.udp.LanDiscoveryService;
import com.oazon.moco.link.core.LinkEventController;
import com.oazon.moco.link.core.LinkSDK;
import com.oazon.moco.link.core.source.LinkEventSource;
import com.oazon.moco.link.ui.LanEventListener;

public class LanServerManage {

	private static Context context = null;
	private static LanServerManage serverManage = null;
	private Intent httpServerIntent = null;
	private Intent discoveryIntent = null;
	public final static String LOCAL_IP = "local_ip";

	private LanEventListener eventListener = new LanEventListener(
			new LinkEventSource()) {

		@Override
		protected void wifiConnectedChange(boolean wificonnected, String wifiName) {
			super.wifiConnectedChange(wificonnected, wifiName);

			Settings.getInstance(context).set(LOCAL_IP, Utils.getLocalIp());
		}

		@Override
		protected void closeService() {
			closeServer();
		}

		@Override
		protected void openService(int port) {
			openServer(port);
		}

	};

	private LanServerManage() {
		if (LanServerManage.context == null) {
			Log.d("Gavin", "LanServerManage++contex++null");
		}
		LinkSDK.getInstance().registLinkListener(eventListener);
		httpServerIntent = new Intent(LanServerManage.context,
				HttpJsonServerService.class);
		NsdService.init(context);
	}

	public static void initServerManage(Context context) {
		LanServerManage.context = context;
		getInstance();

	}

	public synchronized static LanServerManage getInstance() {
		if (serverManage == null) {
			serverManage = new LanServerManage();
		}
		return serverManage;
	}
	
	public void resetNsdServiceName(String name) {
		NsdService.getInstance().resetServiceName(name);
	}

	private void openServer(int port) {
		if (ServiceUtil.isServiceRunning(context,
				"com.oazon.moco.lan.core.http.HttpJsonServerService")) {
			LinkSDK.getInstance().openServiceCallback(
					LinkEventController.lanSource, false);
			return;
		}
		
		NsdService.getInstance().registerService(port, null);
		
		discoveryIntent = new Intent(LanServerManage.context,
				LanDiscoveryService.class);
		LanServerManage.context.startService(discoveryIntent);
		
		httpServerIntent = new Intent(LanServerManage.context,
				HttpJsonServerService.class);
		httpServerIntent.putExtra("port", port);
		LanServerManage.context.startService(httpServerIntent);
	}

	private void closeServer() {

		if (!ServiceUtil.isServiceRunning(context,
				"com.oazon.moco.lan.core.http.HttpJsonServerService")) {
			LinkSDK.getInstance().closeServiceCallback(
					LinkEventController.lanSource, false);
			return;
		}
		NsdService.getInstance().unRegisterService();

		if (discoveryIntent != null) {
			LanServerManage.context.stopService(discoveryIntent);
			discoveryIntent = null;
		}

		if (httpServerIntent != null) {
			LanServerManage.context.stopService(httpServerIntent);
			httpServerIntent = null;
			ControlDriverCache.getInstance().deleteAllControlDriver();
		}

	}
}
